package com.piece.blog.controller;

import com.piece.blog.model.TrainingQuestionTags;
import com.piece.blog.service.TrainingQuestionTagsService;
import com.piece.core.framework.annotation.permission.AclPermission;
import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.constant.ExceptionConstants;
import com.piece.core.framework.support.bootstrap.DataTable;
import com.piece.core.framework.support.bootstrap.QueryTable;
import com.piece.core.framework.support.bootstrap.TreeData;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.string.StringUtil;
import com.piece.core.log.config.UserContextHolder;
import com.piece.core.web.controller.FrameWorkController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 题目标签Controller
 *
 * @author onepiece
 * @date 2023-09-12
 */
@RestController
@Api(tags = {"题目标签管理"})
@RequestMapping("/api/training/exam/question/tags")
public class TrainingQuestionTagsController extends FrameWorkController {

    @Resource
    private TrainingQuestionTagsService trainingQuestionTagsService;

    @PostMapping("/list")
    @AclPermission(code = "training:question:view")
    @ApiOperation(value = "查询题目标签列表")
    @ApiImplicitParam(value = "查询条件", dataTypeClass = QueryTable.class, type = "body")
    public List list() {
        QueryTable queryTable = initTable();
        DataTable dataTable = trainingQuestionTagsService.findPage(queryTable);
        return dataTable.getData();
    }

    /**
     * 加载所有字典列表树
     */
    @PostMapping("/treeData")
    @ApiOperation(value = "加载所有标签列表树")
    @ApiImplicitParam(name = "from", value = "请求来源")
    public List<TreeData> treeData() {
        // 请求来源
        String from = getRequest().getParameter("from");

        List<TrainingQuestionTags> list = trainingQuestionTagsService.findAll();
        List<TreeData> result = trainingQuestionTagsService.findTreeData(list);

        if ("vue".equals(from)) {
            TreeData root = TreeData.builder().id("0").name("全部").build();
            if (result.size() > 1) {
                root.setId(result.get(0).getPId());
            }
            result = findChildTreeData(result, Convert.toStr(root.getId(), "0"));
            root.setChildren(result);
            List<TreeData> convertResult = new ArrayList<>();
            convertResult.add(root);
            return convertResult;
        }
        return result;
    }

    @GetMapping("/load")
    @AclPermission(code = "training:question:view")
    @ApiOperation(value = "加载题目标签表单数据")
    @ApiImplicitParam(name="id", value = "主键")
    public AjaxResponse load() {
        long id = Convert.toLong(getRequest().getParameter("id"), 0L);
        Long pid = Convert.toLong(getRequest().getParameter("pid"), null);
        TrainingQuestionTags tags = new TrainingQuestionTags();
        if (null != pid) {
            tags.setParentId(pid);
        } else if (id != 0) {
            tags = trainingQuestionTagsService.findById(id);
        }
        return success(tags);
    }

    @SubmitPermission
    @PostMapping("/save")
    @AclPermission(code = "training:question:edit")
    @ApiOperation(value = "保存题目标签表单数据")
    @ApiImplicitParam(value = "题目标签数据", dataTypeClass = TrainingQuestionTags.class, type = "body")
    public AjaxResponse save() {
        TrainingQuestionTags tags= new TrainingQuestionTags();
        try {
            initDB(tags);
            if (null != tags.getTagId() && tags.getTagId() != 0L) {
                trainingQuestionTagsService.update(tags);
            } else {
                tags.setCreateBy(Convert.toLong(UserContextHolder.getUserId(), null));
                tags.setCreateTime(new Date());
                trainingQuestionTagsService.insert(tags);
            }
            return success(I18nUtil.message(ExceptionConstants.SAVE_SUCCESS), null);
        } catch (Exception e) {
            return error(I18nUtil.message(ExceptionConstants.SAVE_FAILURE), e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/delete/{id}")
    @AclPermission(code = "training:question:delete")
    @ApiOperation(value = "删除题目标签数据")
    @ApiImplicitParam(name = "id", value = "主键", type = "path")
    public AjaxResponse delete(@PathVariable String id) throws Exception {
        trainingQuestionTagsService.delete(id);
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

    @SubmitPermission
    @PostMapping("/delete")
    @AclPermission(code = "training:question:delete")
    @ApiOperation(value = "删除题目标签数据")
    @ApiImplicitParam(name = "ids", value="主键集合")
    public AjaxResponse deleteBatch() throws Exception {
        String ids = getRequest().getParameter("ids");
        if (StringUtil.isNotEmpty(ids)) {
            trainingQuestionTagsService.delete(Convert.toLongArray(ids));
        }
        return success(I18nUtil.message(ExceptionConstants.DELETE_SUCCESS), null);
    }

}
